Export Multiple Worksheets' Info to One Dbf/Text File

Sandy Radjenovic

Board Regular
Joined
Nov 4, 2003
Messages
76
I have an excel file with nine worksheets. Seven of the worksheets are copies of each other but refer to a different machine with the worksheet name being that of the machine. I would like to do the following in a macro ...

Copy lines from row 7 to bottom of worksheets 2-8 and create one database of all info. Row 6 of any of these worksheets contain the database field names I wish to use. I can even copy the info to a text file if that would be easier.

Beyond my scope of VBA ... Any ideas?
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Okay...this has undergone some very, very light testing, but try it out on your data and see if it works.

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> ExportData2TxtFile()
    <SPAN style="color:#00007F">Dim</SPAN> FieldHeader <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> FieldCount <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
    
    <SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> j <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> k <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
    
    <SPAN style="color:#00007F">Dim</SPAN> f <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>
    
    <SPAN style="color:#00007F">Const</SPAN> DATFILE <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN> = "C:\data_export.dat" <SPAN style="color:#007F00">'change this to your file destination</SPAN>
    
    FieldCount = ThisWorkbook.Sheets(2).Range("$A$6").End(xlToRight).Column

    <SPAN style="color:#00007F">For</SPAN> i = 1 <SPAN style="color:#00007F">To</SPAN> FieldCount
        <SPAN style="color:#00007F">If</SPAN> i = 1 <SPAN style="color:#00007F">Then</SPAN>
            FieldHeader = ThisWorkbook.Sheets(2).Cells(6, i)
        <SPAN style="color:#00007F">Else</SPAN>
            FieldHeader = FieldHeader & "," & ThisWorkbook.Sheets(2).Cells(6, i)
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    <SPAN style="color:#00007F">Next</SPAN>
    
    f = FreeFile
    
    <SPAN style="color:#00007F">Open</SPAN> DATFILE <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Output</SPAN> <SPAN style="color:#00007F">As</SPAN> #f
    
    <SPAN style="color:#00007F">Print</SPAN> #f, FieldHeader
    
    <SPAN style="color:#00007F">For</SPAN> i = 2 <SPAN style="color:#00007F">To</SPAN> 8
        <SPAN style="color:#00007F">For</SPAN> j = 7 <SPAN style="color:#00007F">To</SPAN> ThisWorkbook.Sheets(i).Cells(Rows.Count, 3).End(xlUp).Row
            <SPAN style="color:#00007F">Dim</SPAN> RecordData <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
            
            <SPAN style="color:#00007F">With</SPAN> ThisWorkbook.Sheets(i)
                <SPAN style="color:#00007F">For</SPAN> k = 1 <SPAN style="color:#00007F">To</SPAN> FieldCount
                    <SPAN style="color:#00007F">If</SPAN> k = 1 <SPAN style="color:#00007F">Then</SPAN>
                        RecordData = .Cells(j, k)
                    <SPAN style="color:#00007F">Else</SPAN>
                        RecordData = RecordData & "," & .Cells(j, k)
                    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
                <SPAN style="color:#00007F">Next</SPAN>
            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
        
            <SPAN style="color:#00007F">Print</SPAN> #f, RecordData
        <SPAN style="color:#00007F">Next</SPAN>
    <SPAN style="color:#00007F">Next</SPAN>

    <SPAN style="color:#00007F">Close</SPAN> #f
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>
 
Upvote 0

Forum statistics

Threads
1,215,391
Messages
6,124,678
Members
449,179
Latest member
fcarfagna

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top